export const trim = str => {
  return str.replace(/(^\s*)|(\s*$)/g, "");
}

export const getWordLen = (str) => {
  str += '';

  let realLength = 0, charCode = -1;

  for(let codePoin of str){
    charCode = codePoin.charCodeAt(0);
    if (charCode >= 0 && charCode <= 128) {
      realLength += 1;
    }
    else {
      realLength += 2;
    }
  }

  return realLength;
}

export const getStyle = (option = {}) => {
  if(!option){
    return '';
  }

  let style = [];
  for(let p in option){
    style.push(p + ':' + option[p]);
  }
  return style.join(';');
}

export const addZero = (num) => {
  return ("00" + num).substr(("" + num).length);
}

export const camelCase = (string) => {
  return string ? string.replace(/-\D/g, function (match) {
      return match.charAt(1).toUpperCase();
  }) : '';
}

export const hyphenate = (string) => {
  return string ? string.replace(/[A-Z]/g, function (match) {
      return ('-' + match.charAt(0).toLowerCase());
  }) : '';
}

export const toLocaleString = (num) => {
  if(Number.isNaN(num)){
    return num;
  }

  const all = ('' + num).split('.');
  const integer = all[0];
  const decimal = all[1];
  let str = '';

  if(integer){
    const nums = integer.split('');

    for(let i = nums.length - 3; i > 0; i -=3 ){
      nums.splice(i,0,',');
    }

    str += nums.join('');
  }

  if(decimal){
    const nums = decimal.split('');
    let len = nums.length;
    for(let i = 3; i < len; i +=3 ){
      nums.splice(i,0,',');
      len += 1;
      i += 1;
    }

    str += '.' + nums.join('');
  }

  return str;
}
